/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panneau_Menu extends JPanel_Principal {

	private JButton creerPartieBouton;
	private JButton reprendrePartieBouton;
	private JButton afficherReglesBouton;
	private JButton quitterBouton;
	private JPanel panelBase;
	private GridBagConstraints gbc;
	private Image table;
	private boolean flagInit = true;
	private Fenetre_Principale fenetre;

	public Panneau_Menu(Fenetre_Principale fenetre) {
		super();
		this.fenetre = fenetre;
		this.panelBase = new JPanel();
		this.creerPartieBouton = new JButton("Lancer une nouvelle partie");
		this.reprendrePartieBouton = new JButton("Reprendre une partie");
		this.afficherReglesBouton = new JButton("Règles du jeu");
		this.quitterBouton = new JButton("Quitter");
		this.setBackground(TRANSPARENT);
		this.setOpaque(false);
		this.setLayout(new BorderLayout());
		this.setLayout(new GridBagLayout());
		
		creerPartieBouton.addActionListener(new boutonListener("Creer"));
		reprendrePartieBouton.addActionListener(new boutonListener("Reprendre"));
		quitterBouton.addActionListener(new boutonListener("Quitter"));
		afficherReglesBouton.addActionListener(new boutonListener("Regles"));

		//L'objet servant à positionner les composants
		this.panelBase.setLayout(new GridLayout(4, 1,20,20));
		this.gbc = new GridBagConstraints();
		this.setLayout(new GridBagLayout());
		this.panelBase.add(creerPartieBouton);
		this.panelBase.add(reprendrePartieBouton);
		this.panelBase.add(afficherReglesBouton);
		this.panelBase.add(quitterBouton);
		this.panelBase.setBackground(TRANSPARENT);
		this.panelBase.setOpaque(false);
		gbc.gridy=0;
		gbc.gridx=0;
		gbc.gridwidth = 1;
		gbc.gridheight = 2;
		this.panelBase.setPreferredSize(new Dimension(600, 200));
		this.add(panelBase,gbc);
	}

	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(flagInit){
			Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
			try {
				BufferedImage bufferedFond = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/backgroundTable.jpg"));
				table = bufferedFond.getScaledInstance(screenBounds.width,screenBounds.height, 0);

			} catch (IOException e) {
				e.printStackTrace();
			}
			flagInit = false;
		}
		g.drawImage(table, 0, 0, this);

	}

	class boutonListener implements ActionListener{

		String bouton;
		public boutonListener(String bouton){
			super();
			this.bouton = bouton;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO : Notification au controleur
			switch (bouton) {
			case "Creer" ://Reinitialise la disposition
				fenetre.creerPartieClic();
				break;
			case "Regles":
				URI uri;
				try {
					File htmlFile = new File("res/html/regles/index.html");
					//uri = new URI("res/html/regles/index.html");
					Desktop dt = Desktop.getDesktop();
					dt.browse(htmlFile.toURI());
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				break;
			case "Reprendre" :
                fenetre.chargerPartieClic();
				break;
			case "Quitter" :
				System.exit(0);
				break;
			default :
			}
		}
	}
}
